﻿using System;
using System.ComponentModel;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.Core;

namespace Yuanlin.Business
{
    public class Search : YuanlinUserControl
    {
        #region Private Members

        protected Button searchCompanyButton;
        protected Button searchProductButton;
        protected Button searchZiCaiButton;
        protected ValuedTextBox searchCompanyTextBox;
        protected ValuedTextBox searchCompanyProductTextBox;
        protected ValuedTextBox searchCompanyZiCaiTextBox;

        #endregion

        protected override void OnLoad(EventArgs e)
        {
            searchCompanyButton.Click += new EventHandler(searchCompanyButton_Click);
            searchProductButton.Click += new EventHandler(searchProductButton_Click);
            searchZiCaiButton.Click += new EventHandler(searchZiCaiButton_Click);
            AjaxManager.Register(this, "AjaxMethod");
        }

        private void searchCompanyButton_Click(object sender, EventArgs e)
        {
            string companyName = HttpUtility.HtmlEncode(this.searchCompanyTextBox.Value);
            HttpContext.Current.Response.Redirect(SiteUrls.Instance().GetCompanySimpleSearchResultUrl(companyName));
        }
        private void searchProductButton_Click(object sender, EventArgs e)
        {
            string companyProduct = HttpUtility.HtmlEncode(this.searchCompanyProductTextBox.Value);
            HttpContext.Current.Response.Redirect(SiteUrls.Instance().GetCompanyProductSimpleSearchResultUrl(companyProduct));
        }
        private void searchZiCaiButton_Click(object sender, EventArgs e)
        {
            string companyZiCai = HttpUtility.HtmlEncode(this.searchCompanyZiCaiTextBox.Value);
            HttpContext.Current.Response.Redirect(SiteUrls.Instance().GetZiCaiSimpleSearchResultUrl(companyZiCai));
        }

        #region Ajax Method

        [AjaxMethod(IncludeControlValuesWithCallBack = false)]
        public string GetQueryStr(string userInputStr)
        {
            string returnStr = string.Empty;
            ProductListRequest request = new ProductListRequest();
            request.Title = System.Web.HttpUtility.HtmlEncode(userInputStr);
            request.PageIndex = 0;
            request.PageSize = 50;
            EntityList products = BusinessManager.GetProductList(RequestHelper.BuildGetProductListRequest(request));
            if (products.Count == 0)
            {
                return returnStr;
            }
            string title = string.Empty;
            foreach (Product item in products)
            {
                title += "," + item.Title.Value;
            }

            returnStr = string.Format("{0}", title.Trim(','));

            return returnStr;
        }
        [AjaxMethod(IncludeControlValuesWithCallBack = false)]
        public string GetCompanyQueryStr(string userInputStr)
        {
            string returnStr = string.Empty;
            YuanlinUserListRequest request = new YuanlinUserListRequest();
            request.CompanyName = System.Web.HttpUtility.HtmlEncode(userInputStr);
            EntityList userList = BusinessManager.GetYuanlinUserList(RequestHelper.BuildGetUserListRequest(request));
            if (userList.Count == 0)
            {
                return returnStr;
            }
            string title = string.Empty;
            EntityList users = null;
            foreach (YuanlinUser user in userList)
            {
                users = new EntityList();
                users.Add(user);
                users = RemoveAdminUser(users);

                if (users.Count == 0)
                {
                    continue;
                }
                title += "," + user.CompanyName.Value;
            }

            returnStr = string.Format("{0}", title.Trim(','));

            return returnStr;
        }
        private EntityList RemoveAdminUser(EntityList users)
        {
            EntityList filterUsers = new EntityList();
            foreach (User user in users)
            {
                if (user.EntityId.Value != 10000)
                {
                    filterUsers.Add(user);
                }
            }
            return filterUsers;
        }
        [AjaxMethod(IncludeControlValuesWithCallBack = false)]
        public string GetZiCaiQueryStr(string userInputStr)
        {
            string returnStr = string.Empty;
            ZiCaiInfoListRequest request = new ZiCaiInfoListRequest();
            request.Title = System.Web.HttpUtility.HtmlEncode(userInputStr);
            request.PageIndex = 0;
            request.PageSize = 50;
            EntityList ziCaiInfos = BusinessManager.GetZiCaiInfoList(RequestHelper.BuildGetZiCaiInfoListRequest(request));
            if (ziCaiInfos.Count == 0)
            {
                return returnStr;
            }
            string title = string.Empty;
            foreach (ZiCaiInfo item in ziCaiInfos)
            {
                title += "," + item.Title.Value;
            }

            returnStr = string.Format("{0}", title.Trim(','));

            return returnStr;
        }
        
        #endregion
    }
}
